#include<iostream>
#include<map>
#include<string>
using namespace std;

class User;
class IChatRoom
{
public:
	 IChatRoom(){}
	virtual ~IChatRoom(){}
public:
	virtual void Register(User *pUser) = 0;
	virtual void Send(string fromStr, string toStr, string msg) = 0;
};

class User
{
public:
	User(string name)
	{
		m_sName = name;
	}

	void Send(string toStr, string msg)
	{
		m_pChatRoom->Send(m_sName, toStr, msg);
	}
	void Receive(string fromStr, string msg)
	{
		cout<<fromStr<<" : "<<msg<<endl;
	}
	void SetChatRoom(IChatRoom *pChatRoom)
	{
		m_pChatRoom = pChatRoom;
	}
private:
	friend class ChatRoom;
	IChatRoom *m_pChatRoom;
	string m_sName;
};

class ChatRoom : public IChatRoom
{
public:
	virtual ~ChatRoom()
	{}
	virtual void Register(User *pUser)
	{
		string name =pUser->m_sName ;
		if (m_mapUsers[name] == NULL)
		{
			m_mapUsers[name] = pUser;
			pUser->SetChatRoom(this);
		}
	}
	 
	virtual void Send(string fromStr, string toStr, string msg)
	{
		User *pUser = m_mapUsers[toStr];
		pUser->Receive(fromStr, msg);
	}
private:
	map<string, User*> m_mapUsers;
};

int main5()
{
	IChatRoom *pChatRoom = new ChatRoom();
	User *pUserA = new User("UserA");
	User *pUserB = new User("UserB");

	pChatRoom->Register(pUserA);
	pChatRoom->Register(pUserB);

	pUserA->Send("UserB", "hello UserB");
	delete pUserB;
	delete pUserA;
	delete pChatRoom;
	cin.get();
	return 0;
}
